<template>
    <div>
        <el-container>
            <el-main>
                <h1>用户登录</h1>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                  <el-input type="text" maxlength="12" v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
            
                <el-form-item>
                  <el-button type="primary" @click="loginFromForm('ruleForm')">登录</el-button>
                  <el-button @click="jumpToRegister()">注册</el-button>
                </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>
<script>
export default {
    name: 'Login',
    data() {
        /*var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码!'));
            } else {
                callback();
            }
        };*/
        return {
            ruleForm: {
                username: '',
                password: '',
            },
            rules: {
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur'},
                    {min: 8, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur'},
                    //{ validator: validatePass, trigger: 'blur'}
                ],
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur'},
                    {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        loginFromForm(formName) {
            const _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$axios.post('/user/login', this.ruleForm).then((res) => {
                        console.log(res)
                        if (res.data['code'] != 1) {
                            throw('用户名或密码错误！');
                        }
                        // const token = res.headers['authorization'] // 从返回的结果中获取到 token 信息 // 暂时不测试TOKEN这些
                        // _this.$store.commit('SET_TOKEN', token) // 提交 token 和用户状态信息
                        // _this.$store.commit('SET_USERINFO', res.data.data)
                        console.log('UserId: ', res.data['data']['id']);
                        sessionStorage.setItem('userid', res.data['data']['id']);
                        _this.$router.push('/') // 调整到 /home 路由
                    }).catch(function (error) {
                        console.debug(error);
                        _this.$notify.error({
                            title: '登录失败！',
                            message: error
                        })
                    })
                } else {
                    console.log('error submit')
                    return false;
                }
            });
        },
        jumpToRegister() {
            this.$router.push('/register')
        },
    }
}
</script>